package com.apexsoft.assets.base;

import cn.hutool.core.io.FileUtil;
import cn.hutool.crypto.SecureUtil;
import com.apexsoft.aas.service.model.AUploadRequest;
import com.apexsoft.aas.service.model.UploadFileInfo;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class UploadUtil {
    public static AUploadRequest getUploadRequet(MultipartFile file, String objectName, String columnName, String id) throws IOException {
        AUploadRequest request = new AUploadRequest();
        request.setInputStream(file.getInputStream());
        UploadFileInfo fileInfo = new UploadFileInfo();
        fileInfo.setFileName(file.getOriginalFilename());
        fileInfo.setFileLength(file.getSize());
        String md5 = SecureUtil.md5(file.getInputStream());
        fileInfo.setToken(md5);

        Map<String,Object> params = new HashMap<>();
        params.put("objectName",objectName);
        params.put("columnName",columnName);
        params.put("id",id);

        fileInfo.setParams(params);
        request.setFileInfo(fileInfo);
        return request;
    }

    public static AUploadRequest getUploadRequet(MultipartFile file, Map<String,Object> params ) throws IOException {
        AUploadRequest request = new AUploadRequest();
        request.setInputStream(file.getInputStream());
        UploadFileInfo fileInfo = new UploadFileInfo();
        fileInfo.setFileName(file.getOriginalFilename());
        fileInfo.setFileLength(file.getSize());
        String md5 = SecureUtil.md5(file.getInputStream());
        fileInfo.setToken(md5);

        fileInfo.setParams(params);
        request.setFileInfo(fileInfo);
        return request;
    }

    public static AUploadRequest getUploadRequetByFile(InputStream in, File file, String objectName, String columnName, String id){
        AUploadRequest request = new AUploadRequest();
        request.setInputStream(in);
        UploadFileInfo fileInfo = new UploadFileInfo();
        fileInfo.setFileName(file.getName());
        fileInfo.setFileLength(file.length());
        String md5 = SecureUtil.md5(file);
        fileInfo.setToken(md5);
        Map<String,Object> params = new HashMap<>();
        params.put("objectName",objectName);
        params.put("columnName",columnName);
        params.put("id",id);
        fileInfo.setParams(params);
        request.setFileInfo(fileInfo);
        return request;
    }

    public static AUploadRequest getUploadRequetByStream(InputStream in, String fileName, long fileLength, String objectName, String columnName, String id){
        AUploadRequest request = new AUploadRequest();
        request.setInputStream(in);
        UploadFileInfo fileInfo = new UploadFileInfo();
        fileInfo.setFileName(fileName);
        fileInfo.setFileLength(fileLength);
        Map<String,Object> params = new HashMap<>();
        params.put("objectName",objectName);
        params.put("columnName",columnName);
        params.put("id",id);
        fileInfo.setParams(params);
        request.setFileInfo(fileInfo);
        return request;
    }


    public static AUploadRequest getUploadRequet(MultipartFile file, String objectName, String columnName, String id , Map<String,Object> params) throws IOException {
        AUploadRequest request = new AUploadRequest();
        request.setInputStream(file.getInputStream());
        UploadFileInfo fileInfo = new UploadFileInfo();
        fileInfo.setFileName(file.getOriginalFilename());
        fileInfo.setFileLength(file.getSize());
        String md5 = SecureUtil.md5(file.getInputStream());
        fileInfo.setToken(md5);

        params.put("objectName",objectName);
        params.put("columnName",columnName);
        params.put("id",id);

        fileInfo.setParams(params);
        request.setFileInfo(fileInfo);
        return request;
    }

    public static AUploadRequest getUploadRequetByFile(File file, String objectName, String columnName, String id , Map<String,Object> params){
        AUploadRequest request = new AUploadRequest();
        request.setInputStream(FileUtil.getInputStream(file));
        UploadFileInfo fileInfo = new UploadFileInfo();
        fileInfo.setFileName(file.getName());
        fileInfo.setFileLength(file.length());
        String md5 = SecureUtil.md5(file);
        fileInfo.setToken(md5);

        params.put("objectName",objectName);
        params.put("columnName",columnName);
        params.put("id",id);
        fileInfo.setParams(params);
        request.setFileInfo(fileInfo);
        return request;
    }

//    public static TempFile getTempFileByStream(InputStream inputStream) throws IOException {
//        try{
//            if(inputStream instanceof TempFile){
//                ((TempFile) inputStream).toUnInstallState();
//                ((TempFile) inputStream).toReadState();
//            }
//            TempFile tf = new TempFile();
//            tf.toWriteState();
//            int len = 0;
//            byte[] b = new byte[1024];
//            while ( (len = inputStream.read(b,0,b.length)) != -1){
//                tf.write(b,len);
//            }
//            tf.toReadState();
//            return tf;
//        }finally {
//            if(inputStream!=null){
//                inputStream.close();
//            }
//        }
//
//
//    }

    public static AUploadRequest getUploadMultipartFileRequet(MultipartFile file, Map<String,Object> params ) throws IOException {
        AUploadRequest request = new AUploadRequest();
        request.setInputStream(file.getInputStream());
        UploadFileInfo fileInfo = new UploadFileInfo();
        fileInfo.setFileName(file.getName());
        fileInfo.setFileLength(file.getSize());
        String md5 = SecureUtil.md5(file.getInputStream());
        fileInfo.setToken(md5);
        fileInfo.setParams(params);
        request.setFileInfo(fileInfo);
        return request;
    }
}
